using UnityEngine.UI;
using DG.Tweening;
using UnityEngine;

namespace DL.UI
{
    public class TextData : MonoBehaviour
    {
        [Header("提示文本")]
        public Text Text;
        public CanvasGroup CanvasGroup;
        public RectTransform trans;
        [Header("移动持续时间"),Range(0f,5f)]
        public float MoveDurationTime = 3;
        [Header("渐变持续时间"),Range(0f,5f)]
        public float GradualDurationTime = 3;
        [Header("开始透明度")]
        public float StartDiaphaneity = 1f;
        [Header("结束透明度")]
        public float EndDiaphaneity = 0f;
        [Header("运动曲线")]
        public AnimationCurve RuntimeCurve;
        [Header("开始位置")]
        public float StartPosition = 270;
        [Header("结束位置")]
        public float EndPosition = 450;
        
        private Sequence sequence;

        private float _lastTimestamp;
        private void Create()
        {
            sequence = DOTween.Sequence();
            sequence.Join(DOTween.To(() => StartDiaphaneity, (x) => CanvasGroup.alpha = x, EndDiaphaneity, GradualDurationTime)
                .SetEase(RuntimeCurve));
            sequence.Join(trans.DOAnchorPosY(EndPosition, MoveDurationTime).SetEase(RuntimeCurve)
                .ChangeStartValue(new Vector2(0, StartPosition)));
            sequence.SetUpdate(true);
        }

        public void ReStart(string text, Color color)
        {
            this.Text.text = text;
            this.Text.color = color;
            Create();
            sequence.Restart();
        }

        private void OnDestroy()
        {
            sequence.Kill(true);
        }
    }
}